//: src/com/silvtar/sync/ui/MainFrame.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 4/21/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 4/20/2013   0.1        Create the class
 */

package com.silvtar.sync.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.silvtar.io.StdOut;
import com.silvtar.sync.controller.Daemon;
import com.silvtar.sync.controller.Sync;

/**
 * 主框架，用户第一个见到的界面。
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel            contentPane;

    private JTable            table;

    private JToolBar          toolBar;

    private JMenuBar          menuBar;

    private JMenu             mnConfig;

    private JButton           btnConfig;

    private JButton           btnSyncNow;

    private JTextField        textField;

    private JButton           btnStartdemon;

    private JButton           btnScanclients;

    Daemon                    demon;

    Sync                      sync;

    /**
     * Create the frame.
     */
    public MainFrame() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        mnConfig = new JMenu("Config");
        menuBar.add(mnConfig);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        String headers[] = { "Name", "Path", "LastSync" };
        Object[][] cellData = null;
        DefaultTableModel tableModel = new DefaultTableModel(cellData, headers) {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean isCellEditable(int row, int column) {

                return false;
            }
        };
        table = new JTable(tableModel);
        table.setColumnSelectionAllowed(true);
        scrollPane.setViewportView(table);

        toolBar = new JToolBar();
        contentPane.add(toolBar, BorderLayout.NORTH);

        btnConfig = new JButton("Config");
        btnConfig.addActionListener(new SSActionListener());
        toolBar.add(btnConfig);

        btnSyncNow = new JButton("Sync Now");
        btnSyncNow.addActionListener(new SSActionListener());
        toolBar.add(btnSyncNow);

        btnStartdemon = new JButton("Start Demon");
        btnStartdemon.addActionListener(new SSActionListener());
        toolBar.add(btnStartdemon);

        btnScanclients = new JButton("Scan Clients");
        btnScanclients.addActionListener(new SSActionListener());
        toolBar.add(btnScanclients);

        textField = new JTextField();
        contentPane.add(textField, BorderLayout.SOUTH);
        textField.setColumns(10);

        //
        tableModel.insertRow(tableModel.getRowCount(), new Object[] { "local",
                "/User/henn/sync", "5/20/2013" });
        // 逻辑相关部分
        demon = new Daemon();
        demon.startDaemon();
        sync = new Sync();
    }

    class SSActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            JButton jButton = (JButton) e.getSource();

            // 点击了开启守护进程
            if (jButton.equals(btnStartdemon)) {
                StdOut.println("\nMainFrame: StartDemon Button Pressed!");
                if (null == demon) {
                    demon.startDaemon();
                } else {
                    StdOut.println("Daemon is current running!");
                }
            }
            // 点击了同步按钮
            else if (jButton.equals(btnSyncNow)) {
                StdOut.println("\nMainFrame: SyncNow Button Pressed!");
                sync.syncNow();
            }
            // 点击了配置按钮
            else if (jButton.equals(btnConfig)) {
                StdOut.println("\nMainFrame: Config Button Pressed!");

            }
            // 点击了扫描客户端按钮
            else if (jButton.equals(btnScanclients)) {
                StdOut.println("\nMainFrame: Scan Clients Pressed!");
                demon.printSocketList();
            }
        }

    }
}
